package io.renren.modules.book.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.R;
import io.renren.modules.book.entity.HealthEntity;
import io.renren.modules.book.service.HealthService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;

/**
 * mysql百万级数据量场景
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/health")
public class TestMysqlController {

    @Resource
    HealthService healthService;
    @GetMapping("test")
    public R test(){
        return R.ok().put("状态","测试1234");
    }
    @GetMapping("list")
    public R list(HealthEntity healthEntity){
        Page<HealthEntity> pages = new Page<HealthEntity>(healthEntity.getPage(),healthEntity.getLimit());
        Page<HealthEntity> entityPage = null;
        // username未加索引，查询明显缓慢
        if (StringUtils.isNotEmpty(healthEntity.getHiUserName())){
            entityPage = healthService.page(pages, new QueryWrapper<HealthEntity>().eq("HI_USER_NAME",healthEntity.getHiUserName()).orderByDesc("HI_CREATE_TIME"));
            return R.ok().put("data",entityPage.getRecords());
        }
        // 按单位查，加索引
        if (StringUtils.isNotEmpty(healthEntity.getHiDepName())){
            entityPage = healthService.page(pages, new QueryWrapper<HealthEntity>().eq("HI_DEP_NAME",healthEntity.getHiDepName()).orderByDesc("HI_CREATE_TIME"));
            return R.ok().put("data",entityPage.getRecords());
        }

        // .between("HI_CREATE_TIME", "2022-01-11","2022-01-11 23:59:59")
        entityPage = healthService.page(pages, new QueryWrapper<HealthEntity>().orderByDesc("HI_CREATE_TIME"));
        return R.ok().put("data",entityPage.getRecords());
    }

    @GetMapping("findByDate")
    public R findByDate(HealthEntity healthEntity){
        // 日期加索引  用between 或<> 不用like
        Page<HealthEntity> pages = new Page<HealthEntity>(healthEntity.getPage(),healthEntity.getLimit());
        Page<HealthEntity> entityPage = healthService.page(pages, new QueryWrapper<HealthEntity>().between("HI_CREATE_TIME", healthEntity.getStartTime(),healthEntity.getEndTime()).orderByDesc("HI_CREATE_TIME"));
        return R.ok().put("data",entityPage.getRecords());
    }
}
